<template>
  <a-spin :spinning="confirmLoading">

        <a-tabs defaultActiveKey="1" :activeKey.sync="activeKey" @change='tabsChange'>
          <a-tab-pane tab="合同评审表" key="1">
            <j-form-container :disabled="formDisabled">
              <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail" >
        <a-row>
          <a-col :span="6">
            <a-form-model-item label="评审表编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入评审表编号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="排产单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="schedulingNumber">
              <a-input v-model="model.schedulingNumber" placeholder="请输入排产单号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="合同评审说明" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractCode">
              <a-input v-model="model.contractCode" disabled placeholder="请输入合同评审说明"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">

            <a-form-model-item label="客户名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customerId">
              <j-dict-select-tag type="list" disabled v-model="model.customerId" dictCode="erp_customer,name,id" placeholder="请选择客户" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="客户类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customer">
              <a-input v-model="model.customer" disabled placeholder="请输入客户类型" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="重量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="weight">
              <a-input-number v-model="model.weight" disabled placeholder="请输入重量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="重量单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="weightUnit">
              <j-dict-select-tag type="list" disabled v-model="model.weightUnit" dictCode="zl_unit" placeholder="请选择重量单位" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="币种" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="currency">
              <j-dict-select-tag type="list" v-model="model.currency" dictCode="currency" placeholder="请选择币种" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="是否确认版样" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isPattern">
              <j-dict-select-tag type="list" v-model="model.isPattern" dictCode="yes_no" placeholder="请选择是否确认版样" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="付款方式"  :labelCol="labelCol" :wrapperCol="wrapperCol" prop="payMethod">
              <a-input v-model="model.payMethod" disabled placeholder="请输入付款方式" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="交货期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deliveryTime">
              <a-input v-model="model.deliveryTime" disabled placeholder="请输入交货期" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="定金" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deposit">
              <a-input-number v-model="model.deposit" placeholder="请输入定金" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="已到预付款" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="advancePayment">
              <a-input v-model="model.advancePayment" placeholder="请输入已到预付款"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="定货日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractTime">
              <a-input v-model="model.contractTime" disabled placeholder="请输入定货日期" ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
<!--              <j-editor v-model="model.remark" />-->
              <a-textarea v-model="model.remark" :rows="10" allow-clear showCount ></a-textarea>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="生产备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productionRemark">
<!--              <j-editor v-model="model.productionRemark" />-->
              <a-textarea v-model="model.productionRemark" :rows="10" allow-clear showCount ></a-textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
              </a-form-model>
            </j-form-container>
          </a-tab-pane>

          <a-tab-pane tab="产品及配方" key="2" forceRender>
            <j-form-container :disabled="formDisabled">
              <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail" >
            <a-row :span="24">
              <a-col :span="6">
                <a-form-model-item label="是否保密" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isSecrecy">
                  <j-dict-select-tag type="list" v-model="model.isSecrecy" dictCode="yes_no" placeholder="请输入是否保密"/>
<!--                  <a-input v-model="model.isSecrecy" placeholder="请输入是否保密"  ></a-input>-->
                </a-form-model-item>
              </a-col>
              <a-col :span="6">
                <a-form-model-item label="损耗率(%)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lossRate">
                  <a-input-number v-model="model.lossRate" placeholder="请输入损耗率(%)" style="width: 100%" />
                </a-form-model-item>
              </a-col>
              <a-col :span="6">
                <a-form-model-item label="视比重/粘度(中间值/目标值)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="proportion">
                  <a-input-number v-model="model.proportion" placeholder="请输入视比重/粘度(中间值/目标值)" style="width: 100%" />
                </a-form-model-item>
              </a-col>
              <a-col :span="6">
                <a-form-model-item label="增加费用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isIncrease">
                  <a-input v-model="model.isIncrease" placeholder="请输入增加费用"  ></a-input>
                </a-form-model-item>
              </a-col>

            </a-row>
              </a-form-model>
            </j-form-container>
            <!-- 子表单区域 -->
            <a-row :span="24"  >
              <a-col :span="24" >
                <a-col :span="24">
                  产品信息
                </a-col>
              <!-- 新增 -->
              <a-col :span="24">
                <j-form-container :disabled="formDisabled">
                  <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail" >
                <a-button
                  type="primary"
                  icon=""
                  size="small"
                  @click="addGoods">
                  新增产品
                </a-button>
                  </a-form-model>
                </j-form-container>
                <erp-category-goods-list-modal ref="selectGoodsList"
                                      :modalWidth="modalWidth"
                                      :category="1"
                                      @goodsOk="goodsOk"
                                      checkval='checkbox'>
                </erp-category-goods-list-modal>
              </a-col>
              <a-col :span="24">
                <!-- 表格 -->
                <a-table bordered :data-source="model.erpGoodsPackageList"
                         :columns="goodsColumns"
                         :rowKey="record=>record.goodsId"
                         :selected="onSelectChange"
                         :row-selection='{type: type , selectedRowKeys: selectedRowKeys, onChange: onSelectChange}'
                         :pagination="false">
                  <template slot="number" slot-scope="text, record">
                    <editable-cell :text="text" @change="onCellChange(record, 'number', $event)"></editable-cell>
                  </template>
                  <template slot="isProduction" slot-scope="text, record">
                    <editoption-cell :text="text" :options="openDictOptions" @change="onCellChange(record, 'isProduction', $event)" ></editoption-cell>
                  </template>
                  <template slot="mainQuantity" slot-scope="text, record">
                    <editable-cell :text="text" @change="onCellChange(record, 'mainQuantity', $event)"></editable-cell>
                  </template>
                  <template slot="meUnit" slot-scope="text">
                    <span>{{ unitDictOptions[text] }}</span>
                  </template>
                  <template slot="auxiliaryQuantity" slot-scope="text, record">
                    <editable-cell :text="text" @change="onCellChange(record, 'auxiliaryQuantity', $event)"></editable-cell>
                  </template>
                  <template slot="auUnit" slot-scope="text">
                    <span>{{ zlunitDictOptions[text] }}</span>
                  </template>
                  <template slot="operation" slot-scope="text, record">
                    <j-form-container :disabled="formDisabled">
                      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail" >
                    <a @click="handleCorrelation(record)">关联配方</a>
                    <a-divider type="vertical" />
                    <a-popconfirm
                      title="确认删除?"
                      @confirm="() => onDelete(record.goodsId)"
                    >
                      <a href="javascript:;">删除</a>
                    </a-popconfirm>
                      </a-form-model>
                    </j-form-container>
                    <!--                <a @click="handleEdit(record)">保存</a>-->
                  </template>
                </a-table>
              </a-col>
              </a-col>
              <a-col :span="12"  title="关联包材信息">
                <a-card>
                <a-col :span="24"  style="height: 50px">
                  关联包材信息
                  <a-col :span="24">
                    <j-form-container :disabled="formDisabled">
                      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail" >
                    <a-button
                      type="primary"
                      icon=""
                      size="small"
                      @click="addPackage">
                      新增包材
                    </a-button>
                      </a-form-model>
                    </j-form-container>
                    <erp-category-goods-list-modal ref="selectPackageList"
                                                   :modalWidth="modalWidth"
                                                   :category="2"
                                                   @goodsOk="packageOk"
                                                   checkval='checkbox'>
                    </erp-category-goods-list-modal>
                  </a-col>
                </a-col>
                <!-- 新增 -->

                <a-col :span="24">
                <a-table bordered :data-source="model.packagelList"
                         tableLayout='fixed'
                         :columns="packageColumns"
                         :rowKey="record=>record.detailNo"
                         :pagination="false"
                         :scroll="{ x:800,y:500 }"
                         :span="20">
                  <template slot="isBrought" slot-scope="text, record">
                    <editoption-cell :text="text" :options="openDictOptions" @change="onChildCellChange(record, 'isBrought', $event)" ></editoption-cell>
                  </template>
                  <template slot="lossRate" slot-scope="text, record">
                    <editable-cell :text="text" @change="onChildCellChange(record, 'lossRate', $event)"></editable-cell>
                  </template>
                  <template slot="num" slot-scope="text, record">
                    <editable-cell :text="text" @change="onChildCellChange(record, 'num', $event)"></editable-cell>
                  </template>
                  <template slot="requirements" slot-scope="text, record">
                    <editable-cell :text="text" @change="onChildCellChange(record, 'requirements', $event)"></editable-cell>
                  </template>
                  <template slot="isPattern" slot-scope="text">
                    <span>{{ patternDictOptions[text] }}</span>
                  </template>
                  <template slot="operation" slot-scope="text, record">
                    <a @click="handlePreviewFile(record)">查看版样</a>
                    <j-form-container :disabled="formDisabled">
                      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail" >
                    <a-divider type="vertical" />
                    <a @click="handlePattern(record)">修改版样</a>
                    <input
                      type="file"
                      style="display: none"
                      ref="fileInput"
                      @change="handleFileChange"
                    />
                      </a-form-model>
                    </j-form-container>
                    <!--                <a @click="handleEdit(record)">保存</a>-->
                  </template>
                  <!--              <template slot="formula" slot-scope="text, record">-->
                  <!--                <editable-cell :text="text" @change="onCellChange(record.key, 'name', $event)" />-->
                  <!--              </template>-->
                </a-table>
                </a-col>
                </a-card>
              </a-col>
              <a-col :span="12"  title="配方信息">
                <a-card>
                <a-col :span="24" style="height: 50px">
                  配方信息
                </a-col>

                  <a-col :span="24">
                <a-table bordered :data-source="model.detailList"
                         tableLayout='fixed'
                         :columns="materielColumns"
                         :rowKey="record=>record.detailNo"
                         :pagination="false">
                  <!--              <template slot="formula" slot-scope="text, record">-->
                  <!--                <editable-cell :text="text" @change="onCellChange(record.key, 'name', $event)" />-->
                  <!--              </template>-->
                </a-table>
                  </a-col>
                </a-card>
              </a-col>
            </a-row>
            <j-form-container :disabled="formDisabled">
              <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail" >
            <a-row :span="24"  >
            <a-col :span="12" >
              <a-form-model-item label="配方要求" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="formulaReq">
<!--                <j-editor v-model="model.formulaReq"   disabled/>-->
                <a-textarea v-model="model.formulaReq" :rows="10" allow-clear showCount disabled></a-textarea>
              </a-form-model-item>
            </a-col>
            <a-col :span="12" >
              <a-form-model-item label="包装要求" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="packReq">
<!--                <j-editor v-model="model.packReq" disabled/>-->
                <a-textarea v-model="model.packReq" :rows="10" allow-clear showCount disabled></a-textarea>
              </a-form-model-item>
            </a-col>
            </a-row>
              </a-form-model>
            </j-form-container>
          </a-tab-pane>
          <a-tab-pane tab="出口业务报价单" key="3" forceRender>
            <j-form-container :disabled="formDisabled">
              <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail" >
            <buss-out-let-quote-form @my-event="getMyEvent" ref='outletquote' :dataId='model.id'></buss-out-let-quote-form>
              </a-form-model>
            </j-form-container>
          </a-tab-pane>
          <a-tab-pane tab="状态履历修改" :key="4"  :forceRender="true">
            <a-card :bordered='false'>
              <tsl-history  dataTable="buss_contract_requirement_review"  :dataId="model.id"></tsl-history>
            </a-card>
          </a-tab-pane>
        </a-tabs>

    <goods-formula-modal ref="selectFormulaList"
                         :modalWidth="modalWidth"
                         @goodsOk="formulasOk">
    </goods-formula-modal>
  </a-spin>

</template>

<script>
  import { httpAction, getAction, postAction,getFileAccessHttpUrl } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import ErpCategoryGoodsListModal from "@comp/base/modal/ErpCategoryGoodsListModal";
  import EditableCell from '@comp/table/EditableCell'
  import BussOutLetQuoteForm from '../modules/BussOutLetQuoteForm'
  import EditoptionCell from '@comp/table/EditoptionCell'
  import {initDictOptions} from '@/components/dict/JDictSelectUtil'
  import GoodsFormulaModal from "@comp/base/modal/GoodsFormulaModal";
  import { Base64 } from 'js-base64'
  import store from '@/store'
  import TslHistory from '@comp/tsl/TslHistory.vue'
  import moment from 'dayjs'

  export default {
    name: 'BussContractRequirementReviewForm',
    components: {
      TslHistory,
      ErpCategoryGoodsListModal,
      EditableCell,
      EditoptionCell,
      BussOutLetQuoteForm,
      GoodsFormulaModal
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      /*流程自定义表单数据*/
      customFormData:{
        type:Object,
        default:()=>{return {}},
        required:false
      },
    },
    data () {
      return {
        procSelectRows: [],
        contract:{},
        activeKey: '1',
        modalWidth: 1250,
        category: '',
        goodsId: '',
        packageId: '',
        model:{
          id: '',
          desId: '',
          erpGoodsPackageList: [],
          detailList:[],
          packagelList: [],
          contractCode: '',
          customerId: '',
          customer: '',
          weight: '',
          weightUnit: '',
          payMethod: '',
          deliveryTime: '',
          contractTime: '',
          formulaReq: '',
          packReq: '',
        },
        tabs:{
          activeKey: 1
        },
        selectedRowKeys: [],
        type: 'radio',
        // 选中行
        selectedRows: [],
        goodsColumns: [
          {
            title: '产品编号',
            dataIndex: 'packageCode',
          },
          {
            title: '产品名称',
            dataIndex: 'packageName',
          },
          {
            title: '规格型号',
            dataIndex: 'noneSpec',
          },
          {
            title: '配方序号',
            dataIndex: 'number',
            scopedSlots: { customRender: 'number' },
            width: 150,
          },
          {
            title: '是否生产',
            dataIndex: 'isProduction',
            scopedSlots: { customRender: 'isProduction' },
            width: 120,
          },
          {
            title: '主数量',
            dataIndex: 'mainQuantity',
            scopedSlots: { customRender: 'mainQuantity' },
            width: 150,
          },
          {
            title: '主单位',
            dataIndex: 'meUnit',
            scopedSlots: { customRender: 'meUnit' }
          },
          {
            title: '辅数量',
            dataIndex: 'auxiliaryQuantity',
            scopedSlots: { customRender: 'auxiliaryQuantity' },
            width: 150,
          },
          {
            title: '辅单位',
            dataIndex: 'auUnit',
            scopedSlots: { customRender: 'auUnit' }
          },
          {
            title: '库存',
            dataIndex: 'stockQuantity',
          },
          {
            title: '配方年',
            dataIndex: 'formulaYear',
          },
          {
            title: '配方编码',
            dataIndex: 'formulaCode',
          },
          {
            title: '配方名称',
            dataIndex: 'formulaName',
          },
          {
            title: '操作',
            dataIndex: 'operation',
            scopedSlots: { customRender: 'operation' },
          },
        ],
        materielColumns: [
          {
            title: '编号',
            dataIndex: 'detailNo',
          },
          {
            title: '物料名称',
            dataIndex: 'materielName',
          },
          {
            title: '规格',
            dataIndex: 'spec',
          },
          {
            title: '是否客户自带料',
            dataIndex: 'isClientOwn',
          },
          {
            title: '配方%',
            dataIndex: 'formula',
          },
          {
            title: '实际数量',
            dataIndex: 'actualQuantity',
          },
        ],
        packageColumns: [
          {
            title: '包材编号',
            dataIndex: 'packageCode',
            fixed: 'left',
            width: 120,
          },
          {
            title: '包材名称',
            dataIndex: 'packageName',
            fixed: 'left',
            width: 120,
          },
          {
            title: '是否自带',
            dataIndex: 'isBrought',
            scopedSlots: { customRender: 'isBrought' },
            width: 120,
          },
          {
            title: '规格型号',
            dataIndex: 'noneSpec',
            width: 120,
          },
          {
            title: '包材类别',
            dataIndex: 'packageType',
            width: 120,
          },
          {
            title: '损耗率(%)',
            dataIndex: 'lossRate',
            scopedSlots: { customRender: 'lossRate' },
            width: 120,
          },
          {
            title: '比例数量(外包:内包)',
            dataIndex: 'num',
            scopedSlots: { customRender: 'num' },
            width: 120,
          },
          {
            title: '材质',
            dataIndex: 'materialQuality',
            width: 120,
          },
          {
            title: '尺寸',
            dataIndex: 'packSpec',
            width: 120,
          },
          {
            title: '包装要求',
            dataIndex: 'requirements',
            scopedSlots: { customRender: 'requirements' },
            width: 120,
          },
          {
            title: '是否有版样',
            dataIndex: 'isPattern',
            scopedSlots: { customRender: 'isPattern' },
            width: 120,
          },
          {
            title: '操作',
            dataIndex: 'operation',
            scopedSlots: { customRender: 'operation' },
            width: 110,
          },
        ],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
          style: 'width: 28%;',
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           code: [
              { required: true, message: '请输入评审表编号!'},
           ],
          schedulingNumber: [
            { required: true, message: '请输入排产单号!'},
          ],
        },
        url: {
          add: "/review/bussContractRequirementReview/add",
          edit: "/review/bussContractRequirementReview/edit",
          queryById: "/review/bussContractRequirementReview/queryById"
        },
        openDictOptions:{
        },
        unitDictOptions:{},
        zlunitDictOptions:{},
        patternDictOptions:{}
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
      if(this.customFormData != null && this.customFormData.length>0) {
        console.log("this.customFormData=",this.customFormData);
        this.model = this.customFormData;
        this.edit(this.customFormData)
      }
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.initDictConfig();
      this.editAfter();  //现在子表内容需要
    },
    methods: {
      initDictConfig() {
        console.log("--我才是真的方法!--")
        //初始化字典 - 开关
        initDictOptions('yes_no').then((res) => {
          console.log(res);
          if (res.success) {
            console.log(res.result);
            this.openDictOptions = res.result;
          }
        });
        //初始化字典 - 主单位
        initDictOptions('ji_unit').then((res) => {
          var unitDictOptions = this.unitDictOptions
          console.log(res);
          if (res.success) {
            res.result.forEach(function(item,index){
              unitDictOptions[item.value] = item.label;
            })
          }
          this.unitDictOptions = unitDictOptions
        });
        //初始化字典 - 辅单位
        initDictOptions('zl_unit').then((res) => {
          var zlunitDictOptions = this.zlunitDictOptions
          console.log(res);
          if (res.success) {
            res.result.forEach(function(item,index){
              zlunitDictOptions[item.value] = item.label;
            })
          }
          this.zlunitDictOptions = zlunitDictOptions
        });
        //初始化字典 - 是否有版样
        this.patternDictOptions = {
          '1' : '是',
          '0' : '否',
        }
      },
      tabsChange(activeKey){
        this.activeKey = activeKey;
        this.tabs.activeKey = activeKey;
      },
      getPfCode() {
        postAction('/sys/fillRule/executeRuleByCode/psbbh', {}).then(res => {
          this.model.code = res.result;
        })
      },
      add (rows) {
        this.contract = rows[0];
        // this.edit(this.modelDefault);
        // this.getPfCode();
        console.log("11111" + this.model);
        console.log("--------")
        console.log(this.contract)
        // this.model.contractCode = this.contract.code;
        // this.model.customerId = this.contract.customerId;
        // this.model.customer = this.contract.customer;
        // this.model.weight = this.contract.weight;
        // this.model.weightUnit = this.contract.weightUnit;
        // this.model.payMethod = this.contract.payMethod;
        // this.model.deliveryTime = this.contract.deliveryTime;
        // this.model.contractTime = this.contract.createTime;
        // this.model.desId = this.contract.id;
        // this.model.formulaReq = this.contract.formulaReq;
        // this.model.packReq = this.contract.packReq;
        // this.model.erpGoodsPackageList = []
        // this.model.packagelList = []
        // this.model.detailList = []
        var record = {};
        Vue.set(record, 'desId', this.contract.id)
        Vue.set(record, 'contractCode', this.contract.code)
        Vue.set(record, 'customerId', this.contract.customerId)
        Vue.set(record, 'customer', this.contract.customer)
        Vue.set(record, 'weight', this.contract.weight)
        Vue.set(record, 'weightUnit', this.contract.weightUnit)
        Vue.set(record, 'payMethod', this.contract.payMethod)
        Vue.set(record, 'deliveryTime', this.contract.deliveryTime)
        Vue.set(record, 'contractTime', this.contract.createTime)
        Vue.set(record, 'formulaReq', this.contract.formulaReq)
        Vue.set(record, 'packReq', this.contract.packReq)
        Vue.set(record, 'erpGoodsPackageList', [])
        Vue.set(record, 'detailList', [])
        Vue.set(record, 'packagelList', [])
        this.model = Object.assign({}, record);
      },
      edit (record) {
        // if ()
        getAction('/business/bussContractReviewDes/queryById', {id:record.desId}).then(res => {
          console.log("1111111");
          console.log(res);
          let records = res.result
          if (records != null) {

            Vue.set(record, 'contractCode', records.code)
            Vue.set(record, 'customerId', records.customerId)
            Vue.set(record, 'customer', records.customer)
            Vue.set(record, 'weight', records.weight)
            Vue.set(record, 'weightUnit', records.weightUnit)
            Vue.set(record, 'payMethod', records.payMethod)
            Vue.set(record, 'deliveryTime', records.deliveryTime)
            Vue.set(record, 'contractTime', records.createTime)
            Vue.set(record, 'formulaReq', records.formulaReq)
            Vue.set(record, 'packReq', records.packReq)
            Vue.set(record, 'erpGoodsPackageList', [])
            Vue.set(record, 'detailList', [])
            Vue.set(record, 'packagelList', [])
          }
          this.model = Object.assign({}, record);
          //此处可调用出口业务报价单edit，传入出口业务报价单model
          this.visible = true;

          this.editAfter()
        })
      },
      editAfter () {
        let _this = this;
        console.log("11111" + _this.model.erpGoodsPackageList);
        getAction('/base/erpGoods/goodsAndFormula/ByReviewId', {id:this.model.id}).then(res => {
          let erpGoodsList = res.result;
          erpGoodsList.forEach(function(item,index){
            console.log(item,index)
            let detail = {};
            detail.goodsId = item.id;
            detail.packageCode = item.code;
            detail.packageName = item.name;
            detail.noneSpec = item.noneSpec;
            detail.meUnit = item.meUnit;
            detail.auUnit = item.auUnit;
            detail.formulaId = item.formulaId;
            detail.formulaCode = item.formulaCode;
            detail.formulaName = item.formulaName;
            detail.formulaYear = item.formulaYear;
            detail.packageVos = item.packageVos;
            detail.number = item.number;
            detail.mainQuantity = item.mainQuantity;
            detail.auxiliaryQuantity = item.auxiliaryQuantity;
            detail.isProduction = item.isProduction;
            // _this.filterPackageId(item.id);
            _this.model.erpGoodsPackageList.push(detail);
          })
        })
        if (_this.model.erpGoodsPackageList == null){
          _this.model.erpGoodsPackageList = [];
        }
      },
      getMyEvent() {
        const that = this;
        that.$emit('ok');
      },
      submitForm () {
        //提交
        const that = this;
        console.log(this.tabs)
        if(this.tabs.activeKey == 1){
          this.activeKey = '2';
          this.tabs.activeKey = 2;
          return;
        }
        if(this.tabs.activeKey == 3){
          this.$refs.outletquote.submitForm(this.model.id)
          return;
          // that.$emit('ok');
        }

        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                // that.$message.success(res.message);
                if(this.tabs.activeKey == 2){
                  this.model.id = res.message;
                  this.activeKey = '3';
                  this.tabs.activeKey = 3;
                  return;
                }
                // that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      addGoods: function(){
        // 新增物料
        // console.log(this.$refs.selectGoodsList)
        this.$nextTick(()=>{
          this.$refs.selectGoodsList.add();
        })
        // this.$refs.selectGoodsList.add();
        // this.$refs.selectGoodsList.title = "新增";
        // this.$refs.selectGoodsList.disableSubmit = false;
      },
      goodsOk(datas) {
        let _this = this;
        postAction('/base/erpGoods/goodsAndFormula/ids', { ids: datas }).then(res => {
          console.log(res.result)
          let erpGoodsList = res.result;
          erpGoodsList.forEach(function(item,index){
            console.log(item,index)
            let detail = {};
            detail.goodsId = item.id;
            detail.packageCode = item.code;
            detail.packageName = item.name;
            detail.noneSpec = item.noneSpec;
            detail.meUnit = item.meUnit;
            detail.auUnit = item.auUnit;
            detail.formulaId = item.formulaId;
            detail.formulaCode = item.formulaCode;
            detail.formulaName = item.formulaName;
            detail.formulaYear = item.formulaYear;
            detail.packageVos = item.packageVos;
            // _this.filterPackageId(item.id);
            _this.model.erpGoodsPackageList.push(detail);
          })
        })
      },
      packageOk(datas) {
        console.log(datas)
        let _this = this;
        postAction('/base/erpGoods/goodsAndFormula/ids', { ids: datas }).then(res => {
          console.log(res.result)
          let erpGoodsList = res.result;
          erpGoodsList.forEach(function(item,index){
            console.log(item,index)
            let detail = {};
            detail.packageId = item.id;
            detail.packageCode = item.code;
            detail.packageName = item.name;
            detail.noneSpec = item.noneSpec;
            detail.meUnit = item.meUnit;
            detail.auUnit = item.auUnit;
            detail.packageType = item.packageType;
            detail.materialQuality = item.materialQuality;
            detail.packSpec = item.packSpec;
            detail.isPattern = item.isPattern;
            const dataSource = [..._this.model.erpGoodsPackageList];
            const target = dataSource.find(item => item.goodsId === _this.selectedRows[0].goodsId);
            if (target.packageVos != null && target.packageVos.length >0 ){
            }else {
              target.packageVos = [];
            }
            target.packageVos.push(detail);
            _this.model.packagelList = target.packageVos;
            // _this.filterPackageId(item.id);
            // _this.model.erpGoodsPackageList.push(detail);
          })
        })
      },
      addPackage: function(){
        if (this.selectedRows.length < 1) {
          this.$message.error("请先选择产品");
          return;
        }
        // 新增物料
        // console.log(this.$refs.selectGoodsList)
        this.$nextTick(()=>{
          this.$refs.selectPackageList.add();
        })
        // this.$refs.selectGoodsList.add();
        // this.$refs.selectGoodsList.title = "新增";
        // this.$refs.selectGoodsList.disableSubmit = false;
      },
      filterPackageId(goodsId){
        this.model.erpGoodsPackageList = this.model.erpGoodsPackageList.filter(function(item) {
          return item.goodsId !== goodsId;
        });
      },
      onCellChange(record, dataIndex, value) {
        console.log(this.model.erpGoodsPackageList)
        console.log(record)
        const dataSource = [...this.model.erpGoodsPackageList];
        const target = dataSource.find(item => item.packageCode === record.packageCode);
        // console.log(target,key,dataIndex,value)
        if (record) {
          target[dataIndex] = value;
          this.model.erpGoodsPackageList = dataSource;
        }
      },
      onChildCellChange(record, dataIndex, value) {
        console.log(this.model.erpGoodsPackageList)
        console.log(record)
        const dataSource = [...this.model.erpGoodsPackageList];
        const target = dataSource.find(item => item.goodsId === this.selectedRows[0].goodsId);
        const packageSource = target.packageVos;
        const packagetarget = packageSource.find(item => item.packageCode === record.packageCode);
        // console.log(target,key,dataIndex,value)
        if (record) {
          packagetarget[dataIndex] = value;
          this.model.erpGoodsPackageList = dataSource;
        }
        console.log(this.model.erpGoodsPackageList)
      },
      onDelete(key){
        const dataSource = [...this.model.erpGoodsPackageList];
        this.model.erpGoodsPackageList = dataSource.filter(item => item.goodsId !== key);
        // deleteAction('base/erpGoodsFormulaDetail/delete',{id:key}).then(res=>{
        //   if(res.success){
        //
        //   }
        // })

      },
      onSelectChange(selectedRowKeys, selectedRows) {
        if (this.selectedRows.length > 0 && this.selectedRows[0] != null) {
          const key = 'goodsId'
          this.selectedRowKeys = [selectedRowKeys.filter(item => item !== this.selectedRowKeys[0])[0]]
          this.selectedRows = [selectedRows.filter(item => item[key] !== this.selectedRows[0][key])[0]]
        } else {
          this.selectedRowKeys = [selectedRowKeys[0]]
          this.selectedRows = [selectedRows[0]]
        }
        console.log(this.selectedRows)
        if (this.selectedRows[0] != null) {
          getAction('/base/erpGoodsFormulaDetail/list', { formulaCode: this.selectedRows[0].formulaCode }).then(res => {
            this.model.detailList = res.result;
          })
          const dataSource = [...this.model.erpGoodsPackageList];
          const target = dataSource.find(item => item.goodsId === this.selectedRows[0].goodsId);
          if (target.packageVos != null && target.packageVos.length >0 ){
            this.model.packagelList = target.packageVos;
          }else {
            this.model.packagelList = [];
          }
        }
      },
      handleCorrelation(record){
        this.goodsId = record.goodsId;
        this.$refs.selectFormulaList.add();
        this.$refs.selectFormulaList.title = "选择配方";
        this.$refs.selectFormulaList.disableSubmit = false;
      },
      formulasOk(datas) {
        console.log(datas)
        const dataSource = [...this.model.erpGoodsPackageList];
        const target = dataSource.find(item => item.goodsId === this.goodsId);
        if (target) {
          target['formulaId'] = datas.id;
          target['formulaYear'] = datas.year;
          target['formulaName'] = datas.name;
          target['formulaCode'] = datas.code;
          // target['formulaNo'] = datas.no;
          this.model.erpGoodsPackageList = dataSource;
        }
        console.log("111111111111111111111111")
        console.log(this.model.erpGoodsPackageList)
      },
      handlePreviewFile(row) {
        console.log(row)
        getAction('/pattern/erpGoodsPattern/detail', { id: row.packageId }).then(res => {
          let filePath=res.result.name;
          let reviewUrl = this.getImgView(filePath)
          console.log(window._CONFIG, "window._CONFIG");
          let rowpath = reviewUrl.replace(/\\/g, '/');
          console.log('rowpath:'+rowpath)
          let base = window._CONFIG.onlinePreviewDomainURL + "/onlinePreview?url="
          let codename = Base64.encode(rowpath);
          let url = base + codename + '&watermarkTxt=' + encodeURIComponent(store.getters.userInfo.username)
          console.log(url)
          window.open(url, '_blank')
        })

      },
      getImgView(text){
        if(text && text.indexOf(",")>0){
          text = text.substring(0,text.indexOf(","))
        }
        return getFileAccessHttpUrl(text)
      },
      handlePattern(row){
        this.packageId = row.packageId;
        this.$refs.fileInput.click(); // 触发文件上传

      },
      handleFileChange(event) {
        const file = event.target.files[0]; // 获取文件
        if (file) {
          // 处理文件，例如上传到服务器
          this.uploadFile(file);
        }
      },
      uploadFile(file) {
        // 这里编写文件上传的逻辑
        console.log('Uploading file...', file);
        // 示例中仅打印文件信息，实际应用中需要编写文件上传代码
        const formData = new FormData();
        formData.append('file', file);

        // 使用axios发送文件
        postAction('/sys/common/upload', formData, {
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        })
          .then(response => {
            console.log("response");
            console.log(response.message);
            let detail = {};
            detail.patternId = this.packageId;
            detail.name = response.message;
            postAction('/pattern/erpGoodsPattern/addPattern',detail).then(res => {
              this.$message.success(res.message);
            })
          })
          .catch(error => {
            console.error(error);
          });
      },
      // addProc(){
      //   if(this.procSelectRows.length>0){
      //     const rows = this.procSelectRows
      //     this.$nextTick(()=>{
      //       this.contract = rows[0];
      //       this.add();
      //     })
      //   }
      // },
    }
  }
</script>